package site.yunnong.atvris.portal.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.common.utils.FrameGrabberUtils;
import site.yunnong.atvris.common.utils.IDUtils;
import site.yunnong.atvris.dao.domain.UserProfile;
import site.yunnong.atvris.dao.domain.Video;
import site.yunnong.atvris.dao.domain.VideoProfile;
import site.yunnong.atvris.portal.service.UploadService;
import site.yunnong.atvris.service.UserProfileService;
import site.yunnong.atvris.service.VideoProfileService;
import site.yunnong.atvris.service.VideoService;
import site.yunnong.atvris.common.enumeration.MediaStatusEnum;
import site.yunnong.atvris.common.enumeration.MediaTypeEnum;
import site.yunnong.atvris.common.dto.VideoUploadParam;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author zjh
 * @date 2021/8/2 9:57
 */
@Slf4j
@Service
public class UploadServiceImpl implements UploadService {
    
    @Resource
    private VideoService videoService;
    
    @Resource
    private VideoProfileService videoProfileService;
    
    @Resource
    private UserProfileService userProfileService;
    
    @Override
    public int uploadVideo(VideoUploadParam param) {
        // 表单参数校验
        validVideoUploadParam(param);
        // 构建视频对象
        Video video = buildVideo(param);
        // 存视频
        videoService.insert(video);
        // 存视频统计信息
        VideoProfile videoProfile = buildVideoProfile(video.getId());
        videoProfileService.insert(videoProfile);
        // 更新用户总视频个数
        UserProfile userProfile = userProfileService.getUserProfileByUuid(param.getUuid());
        userProfileService.addVideoCount(userProfile);
        return 1;
    }

    private VideoProfile buildVideoProfile(Long uvid) {
        return VideoProfile.builder().uvid(uvid)
                .clickCount(0)
                .viewCount(0)
                .likedCount(0)
                .starCount(0)
                .commentCount(0)
                .shareCount(0)
                .build();
    }

    private Video buildVideo(VideoUploadParam param) {
        Video video = Video.builder().uuid(param.getUuid())
                .userAvatar(param.getUserAvatar())
                .userNickname(param.getUserNickname())
                .pid(param.getPid())
                .partitionName(param.getPartitionName())
                .level(param.getLevel())
                .status(MediaStatusEnum.REVIEW_ISSUE.getValue())
                .url(param.getUrl())
                .cover(param.getCover())
                .seriesNum(param.getSeriesNum())
                .title(param.getTitle())
                .videoType(param.getVideoType())
                .rePrintPower(param.getRePrintPower())
                .remark(param.getRemark())
                .uploadTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        if (param.getVideoType() == 0) {
            video.setFromUrl(param.getFromUrl());
        }
        long duration = 0;
        try {
            duration = FrameGrabberUtils.getVideoTime(param.getUrl());
        } catch (IOException e) {
            log.error("UploadServiceImpl 计算duration异常，url: {}, Exception: {}", param.getUrl(), e.getMessage());
        }
        video.setUvid(IDUtils.getId());
        video.setVideoTime(duration);
        return video;
    }

    private void validVideoUploadParam(VideoUploadParam upload) {
        if (Objects.isNull(upload.getUrl())) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "未上传视频");
        }
        if (MediaTypeEnum.VIDEO.getValue().equals(upload.getVideoType())) {
            // 转载
            if (StringUtils.isEmpty(upload.getFromUrl())) {
                // 未填转载自url
                throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "转载视频请输入源视频的链接");
            }
            if (upload.getRePrintPower() == 0) {
                throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "转载视频不可设置为不可转载");
            }
        }
    }
}
